/*eslint no-alert: 0*/
define([
    'dojo/on',
    'dojo/_base/lang',
    'dojo/date/locale',
    'dojo/number',
    'esri/geometry/geometryEngine'
], function (on, lang, locale, number, geometryEngine) {

    function formatDateTime (value) {
        if (value instanceof Date) {
            return locale.format(value, {
                formatLength: 'short'
            });
        }
        return '';
    }

    function formatDate (value) {
        if (value instanceof Date) {
            return locale.format(value, {
                selector: 'date',
                formatLength: 'medium'
            });
        }
        return '';
    }

    function getDateTime (value) {
        if (isNaN(value) || value === 0 || value === null) {
            return null;
        }
        return new Date(value);
    }

    return {
        map: true,
        topicID: 'searchWidget',
        mapClickMode: true,

        queryStringOptions: {
            valueParameter: 'NAME'
        },

        enableAdvancedSearch: true,

        layers: [
            {
                name: '控详规划',
                expression: '', // additional where expression applied to all queries
                idProperty: 'objectid',
                labelWidth: 110,
                queryParameters: {
                    type: 'spatial', // spatial, relationship, table or database
                    layerID: 'controlPlan', // from operational layers
                    sublayerID: 0,
                    outFields: ['*']
                },
                infoTemplates: {
                    buffer: {
                        title: '周边查询',
                        content: function (feature) {
                            if (feature.geometry) {
                                return number.format(geometryEngine.geodesicArea(feature.geometry, 'acres'), {
                                    places: 2
                                }) + ' Acres';
                            }
                            return '';
                        }
                    }
                },
                attributeSearches: [
                    {
                        name: '控详规划',
                        topicID: 'controlPlan',
                        searchFields: [
                            {
                                field: 'GLOBEPLANID',
                                label: '全局地块编号',
                                expression: '(GLOBEPLANID LIKE \'%[value]%\')',
                                width: 'calc(100% - 130px)'
                            },
                            {
                                field: 'ZONEID',
                                label: '规划区编号',
                                expression: '(ZONEID LIKE \'%[value]%\')',
                                values: ['*', '123', '231', '312'],
                                width: 100
                            },
                            {
                                field: 'BLOCKID',
                                label: '街坊号',
                                expression: '(BLOCKID LIKE \'%[value]%\')',
                                values: ['*', '123', '231', '312'],
                                width: 100
                            }
                        ],
                        title: '查询结果'
                    }
                ]
            },
            {
                name: '土地收储',
                expression: '', // additional where expression applied to all queries
                idProperty: 'OBJECTID',
                attributeSearches: [
                    {
                        name: '国有土地',
                        queryParameters: {
                            type: 'table', // spatial, relationship, table or database
                            layerID: 'landStore', // from operational layers
                            sublayerID: 5,
                            outFields: ['*']
                        },
                        searchFields: [
                            {
                                field: 'LAND_CODE',
                                label: '地块编号',
                                expression: '(LAND_CODE LIKE \'%[value]%\')',
                                required: true,
                                minChars: 1,
                                defaultValue: '',
                                width: 'calc(100% - 65px)'
                            },
                            {
                                field: 'LAND_NAME',
                                label: '地块名称',
                                expression: '(LAND_NAME LIKE \'%[value]%\')',
                                required: true,
                                minChars: 1,
                                defaultValue: '',
                                width: 'calc(100% - 65px)'
                            }
//                          {
//                              field: 'Total Admissions',
//                              label: 'Admissions <=',
//                              type: 'numberspinner',
//                              expression: '(TOTALADM <= [value])',
//                              constraints: {min: 1, max: 99999, places: 0},
//                              defaultValue: 4000,
//                              width: 120
//                          }
                        ],

                        title: '查询结果',
                        topicID: 'landStore',
                        gridOptions: {
                            columns: [
                                {
                                    id: 'Action',
                                    field: 'OBJECTID',
                                    label: 'Action',
                                    width: 60,
                                    sortable: false,
                                    exportable: false,
                                    renderCell: function (object, value, node) {
                                        on(node, 'click', function () {
                                            alert('Do something exciting here like search for related records or edit the selected record.');
                                        });
                                        node.innerHTML = '<i class=\'fa fa-pencil\' style=\'margin-left:8px;\'></i>';
                                    }
                                },
                                {
                                    field: 'LAND_CODE',
                                    label: '地块编号'
                                },
                                {
                                    field: 'LAND_NAME',
                                    label: '地块名称',
                                    width: 150
                                },
                                {
                                    field: 'COMPANY_NAME',
                                    label: '企业名称',
                                    width: 150
                                }
//                              {
//                                  field: 'LASTUPDATE',
//                                  label: 'Last Update',
//                                  width: 120,
//                                  get: function (object) { // allow export as a proper date
//                                      return getDateTime(object.LASTUPDATE);
//                                  },
//                                  formatter: formatDateTime
//                              }
                            ],
                            sort: [
                                {
                                    property: 'LAND_CODE',
                                    descending: 'ASC'
                                }
                            ]
                        }
                   }
                ]
            }
        ]
    };
});